package top.makeprogress.taobaospider;

import com.alibaba.fastjson.JSON;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import top.makeprogress.taobaospider.Entity.BoughtRootBean;
import top.makeprogress.taobaospider.Entity.MainOrders;
import top.makeprogress.taobaospider.Entity.Page;
import top.makeprogress.taobaospider.config.ConfigProperties;
import top.makeprogress.taobaospider.config.TaobaoSpilderConfig;
import top.makeprogress.taobaospider.parser.TaoBaoAllOrderParser;
import top.makeprogress.taobaospider.util.DateUtils;
import top.makeprogress.taobaospider.util.LogPrintUtils;
import top.makeprogress.taobaospider.util.RandomUtil;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author liuyi
 * @create 2019-10-11 19:11
 **/

public class Bootstrap {
    public static void initData() {
        System.getProperty("start");
        System.getProperty("end");
    }

    public static void main(String[] args) {
        Document doc = null;
        String cookies = System.getProperty("cookies") != null ? System.getProperty("cookies") : " cookie2=1e5449576bc14623d85e139e66c609be; t=6b0b23cea84a93ea7c4453dec662687c; _tb_token_=e60f3eeb533f8; cna=2oZ0FBss4kUCAXdtb2ieXAyd; v=0; unb=788897000; uc3=lg2=Vq8l%2BKCLz3%2F65A%3D%3D&vt3=F8dByuDnwruRXYbCJ3I%3D&id2=VAmie0yOtVRK&nk2=F5REN0UrKIZ3NC9eH84%3D; csg=14e5d0d7; lgc=tb1911216_2011; cookie17=VAmie0yOtVRK; dnk=tb1911216_2011; skt=602589008dc1e77e; existShop=MTU3MDc5MjcyNw%3D%3D; uc4=nk4=0%40FY4PZdcLfh1w3M0yjM6EZf1gPz6WCnYchA%3D%3D&id4=0%40VhCZQpmN4AvamIDg5eGqqn6Wx3k%3D; tracknick=tb1911216_2011; _cc_=VFC%2FuZ9ajQ%3D%3D; tg=0; _l_g_=Ug%3D%3D; sg=10a; _nk_=tb1911216_2011; cookie1=UIGgSNG7BytUWaQTEn%2FcZc13iqiDmhmzPHlSrm6ZHo0%3D; mt=ci=1_1; thw=cn; ucn=center; uc1=cookie14=UoTbnV%2BAQXrCRw%3D%3D&lng=zh_CN&cookie16=WqG3DMC9UpAPBHGz5QBErFxlCA%3D%3D&existShop=false&cookie21=UtASsssme%2BBq&tag=8&cookie15=UIHiLt3xD8xYTw%3D%3D&pas=0; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; x5sec=7b2274726164656d616e616765723b32223a2263326563366665393535303130396435663861323339333731356333363965364350374f67653046454f7a636b36626b2b4d483762426f4c4e7a67344f446b334d4441774f7a453d227d; whl=-1%260%260%261570794179724; l=cBLNUAquq2MvoVafBOfZRurza77OXQRfGPNzaNbMiICPOKCJlKHlWZIv_7TvCnhVK62vR3Wm0ejuB8LkByCqnzTdqAdnwN5..; isg=BGxssmJiHZ-ocQmjE59YTXRhPUqeJRDP_Hj4B8avhpeI0Qjb7jDLX1wn8NlMoUgn";
        Integer startIndex = System.getProperty("start") != null ? Integer.valueOf(System.getProperty("start")) : 1;
        Integer endIndex = System.getProperty("end") != null ? Integer.valueOf(System.getProperty("start")) : 1000;
        List<MainOrders> mainOrders = new ArrayList<>();
        try {
            //先获取当前订单总条数
            BoughtRootBean boughtRootBean = TaoBaoAllOrderParser.boughtMessgeGet(cookies, 1, 1);
            Page page = boughtRootBean.getPage();
            LogPrintUtils.print("当前用户订单总量为:" + page.getTotalNumber());
            if (endIndex > page.getTotalNumber()) {
                endIndex = page.getTotalNumber();
            }
            //小于最大值数字就直接导出,大于最大值数字的话后续添加缓存方法,分段导出(默认10000)
            if (page.getTotalNumber() < TaobaoSpilderConfig.getOrderMax()) {
                LogPrintUtils.print("当前用户订单总量小于" + TaobaoSpilderConfig.getOrderMax() + "直接导出");
                Integer orderCycle = TaobaoSpilderConfig.getOrderCycle();
                while (startIndex < endIndex) {
                    if (startIndex + orderCycle > endIndex) {
                        orderCycle = endIndex - startIndex;
                    }
                    boughtRootBean = TaoBaoAllOrderParser.boughtMessgeGet(cookies, startIndex, orderCycle);
                    TimeUnit.MILLISECONDS.sleep(TaobaoSpilderConfig.getIntervalTime() + RandomUtil.getRandomLessThan(TaobaoSpilderConfig.getIntervalTime()));
                    mainOrders.addAll(boughtRootBean.getMainOrders());
                    LogPrintUtils.print("第" + startIndex + "到第" + (startIndex + orderCycle) + "条获取成功!");
                    startIndex += orderCycle;
                }
            }
            System.out.println(JSON.toJSONString(mainOrders));
            //将当前订单的信息以文本形式存储到txt中,每10000条导出成一个excel
        } catch (IOException e) {
            LogPrintUtils.printErr("获取连接失败:" + e.getMessage());
        } catch (InterruptedException e) {
            LogPrintUtils.printErr("sleep失败:" + e.getMessage());
        }

    }


}